$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tcactivity/list?status=0',
        datatype: "json",
        colModel: [
			{ label: '活动名称', name: 'activityName', index: 'ACTIVITY_NAME', width: 80 }, 			
			{ label: '活动最大人数', name: 'activityMaxNum', index: 'ACTIVITY_MAX_NUM', width: 80 }, 			
			{ label: '活动积分', name: 'activityIntegral', index: 'ACTIVITY_INTEGRAL', width: 80 },
			{ label: '申请人', name: 'applicantId', index: 'APPLICANT_ID', width: 80 },
			{ label: '联系人', name: 'contactsId', index: 'CONTACTS_ID', width: 80 }, 			
			{ label: '联系人电话', name: 'contactsPhone', index: 'CONTACTS_PHONE', width: 80 },
			{ label: '活动地址', name: 'activityAddress', index: 'ACTIVITY_ADDRESS', width: 80 },
			{ label: '开始时间', name: 'startTime', index: 'START_TIME', width: 80 },
			{ label: '结束时间', name: 'endTime', index: 'END_TIME', width: 80 },
            { label: '活动状态', name: 'activityStatus', index: 'ACTIVITY_STATUS', width: 70,formatter: function(value, options, row){
                    if(value == "APPLY_UN_AUDIT"){
                        return '<span class="label label-success">申请待审核</span>'
                    }else if(value == "APPLY_CANCEL"){
                        return '<span class="label label-success">申请审核取消</span>'
                    }else if(value == "APPLY_AUDIT_FAIL"){
                        return '<span class="label label-success">申请审核未通过</span>'
                    }}},
        ],
		viewrecords: true,
        height: 580,
        rowNum: 50,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }

    });


});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tcActivity: {contactsId:null,contactsPhone:null,startTime:null,endTime:null,activityAddress:null}
	},
	methods: {

        showMemberPage: function(event){

            layer.open({
                type: 2,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择会员",
                area: ['1100px', '500px'],
                shade: 0,
                shadeClose: true,
                content: 'http://localhost:8080/renren-admin/modules/sys/tcmember_load.html',
                btn: ['确定', '取消'],
                btn1: function (index,layero) {
                    var ibody = layer.getChildFrame ('body',index); //获取iframe页面body
                    var tr = ibody.find("#jqGrid tr[class*='success']");
                    var phone = tr.find("td:eq(2)").text();
                    var memberName = tr.find("td:eq(3)").text();
                    if(tr.length>1){
                        alert("只能选择一名会员");
                        return;
                    } else if(tr.length==0){
                        alert("请选择一名会员");
                        return;
                    }

                    /*alert(memberName);*/
                    vm.tcActivity.contactsId=memberName;
                    vm.tcActivity.contactsPhone=phone;
                    $("#memberName").val(memberName);
                    $("#memberPhone").val(phone);
                    layer.close(index);
                }
            });
        },

        showactivityAddress: function(event){
            layer.open({
                type: 2,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择会议室",
                area: ['1100px', '500px'],
                shade: 0,
                shadeClose: true,
                content: 'http://localhost:8080/renren-admin/modules/sys/tcmeetingroom_load.html',
                btn: ['确定', '取消'],
                btn1: function (index,layero) {
                    var ibody = layer.getChildFrame ('body',index); //获取iframe页面body
                    var tr = ibody.find("#jqGrid tr[class*='success']");
                    var activityAddress = tr.find("td:eq(3)").text();
                    if(tr.length>1){
                        alert("只能选择一个会议室");
                        return;
                    } else if(tr.length==0){
                        alert("请选择一个会议室");
                        return;
                    }
                    var sta = "禁用"
                    if (status==sta){
                        alert("请勿选择禁止的会议室");
                        return;
                    }
                    vm.tcActivity.activityAddress=activityAddress;
                    $("#activityAddress").val(activityAddress);
                    layer.close(index);
                }
            });
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tcActivity = {};

		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.tcActivity.id == null ? "sys/tcactivity/save" : "sys/tcactivity/update";
                vm.tcActivity.startTime=$("#test5").val();
                vm.tcActivity.endTime=$("#test6").val();
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.tcActivity),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/tcactivity/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
        apl: function(event){

            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var status1 =$("#jqGrid tr[class*='success']").find("td:last span").text();
            var phone =$("#jqGrid tr[class*='success']").find("td:eq(7)").text();
            var name =$("#jqGrid tr[class*='success']").find("td:eq(2)").text();
            var applicantId =$("#jqGrid tr[class*='success']").find("td:eq(5)").text();
            var concatId =$("#jqGrid tr[class*='success']").find("td:eq(6)").text();
            var address =$("#jqGrid tr[class*='success']").find("td:eq(8)").text();
            //alert(""+phone);
            if(status1=="申请审核取消"){
                layer.alert("申请审核已取消，无须审核");
            }else if(status1=="申请审核未通过"){
                layer.alert("申请审核未通过，无须审核");
            }else {
                layer.open({
                    type: 1
                    ,title: false //不显示标题栏
                    ,closeBtn: 1
                    ,area: '400px;'
                    ,shade: 0.8
                    ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                    ,btn: ['审核通过', '审核未通过','申请审核取消']
                    ,btnAlign: 'c'
                    ,moveType: 1 ,//拖拽模式，0或者1
                    content:'<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">活动申请审核<br/>活动名称:    '+name+'<br/>活动申请人：     '+applicantId+'</div>'
                    ,
                    btn1: function (index) {
                        layer.confirm('确定要审核通过吗？', {
                            btn: ['确定','取消'] //按钮
                        },function () {
                            var data ="cname="+name+"&address="+address+"&concatId="+concatId+"&status=ACTIVITY_AUDIT_PASS&id="+id+"&phone="+phone+"&smstext=您好，您申请的活动 “"+name+"” 已审核通过！请您前往社区，领取占用活动场地 “"+address+"” 的凭证！ 【领航社区】";
                            $.ajax({
                                type: "POST",
                                url: baseURL +"sys/tcactivity/shenhe",
                                data: data,
                                dataType: "json",
                                success: function(result){
                                    if(result.code == 0){
                                        layer.close(index);
                                        layer.alert('申请审核成功通过', function(index){
                                            location.reload();
                                        });
                                    }else{
                                        layer.alert(result.msg);
                                    }
                                }
                            });
                        });
                    },
                    btn2: function (index) {
                        layer.confirm('确定要审核不通过吗？', {
                            btn: ['确定','取消'] //按钮
                        },function () {
                            var data = "status=APPLY_AUDIT_FAIL&id="+id+"&phone="+phone+"&smstext=您好，您申请的活动 “"+name+"” 未通过！ 【领航社区】";
                            $.ajax({
                                type: "POST",
                                url:  baseURL +"sys/tcactivity/shenhe",
                                data: data,
                                dataType: "json",
                                success: function(result){
                                    if(result.code == 0){
                                        layer.close(index);
                                        layer.alert('申请审核成功未通过', function(index){
                                            location.reload();
                                        });
                                    }else{
                                        layer.alert(result.msg);
                                    }
                                }
                            });
                        });
                        return false;
                    },
                    btn3: function (index) {
                        layer.confirm('确定要取消申请活动吗？', {
                            btn: ['确定','取消'] //按钮
                        },function () {
                            var data = "status=APPLY_CANCEL&id="+id+"&phone="+phone+"&smstext=您好，您申请的活动 “"+name+"” 已审核取消！ 【领航社区】";
                            $.ajax({
                                type: "POST",
                                url:  baseURL +"sys/tcactivity/shenhe",
                                data: data,
                                dataType: "json",
                                success: function(result){
                                    if(result.code == 0){
                                        layer.close(index);
                                        layer.alert('申请审核成功取消', function(index){
                                            location.reload();
                                        });
                                    }else{
                                        layer.alert(result.msg);
                                    }
                                }
                            });
                        });
                        return false;
                    }
                });
            }
        },
		getInfo: function(id){
			$.get(baseURL + "sys/tcactivity/info/"+id, function(r){
                vm.tcActivity = r.tcActivity;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});